package com.coocaa.ops.admin.v1.policygroup.service.impl;


import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.audit.AuditUtil;
import com.coocaa.ops.admin.v1.policygroup.convert.PolicyGroupSaveConverter;
import com.coocaa.ops.admin.v1.policygroup.model.DeleteResultModel;
import com.coocaa.ops.admin.v1.policygroup.provider.IPolicyGroupInfoUpdateProvider;
import com.coocaa.ops.admin.v1.policygroup.service.IPolicyGroupInfoUpdateService;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.audit.entity.AuditTaskInfoEntity;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.core.biz.cache.service.MultiCenterCacheUpdateService;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author bjh
 * @description 策略组信息原始表-更新service接口实现
 * @date 2020-03-12 14:46:04
 */
@Service
public class PolicyGroupInfoUpdateServiceImpl implements IPolicyGroupInfoUpdateService {
    @Autowired
    private IPolicyGroupInfoUpdateProvider thisUpdateProvider;
    //    @Autowired
//    private SendEmailServiceImpl sendEmailService;
    @Autowired
    private MultiCenterCacheUpdateService myUpdateCacheService;
    private static CommonLanguageProperties commonLanguageProperties;

    /**
     * 保存
     *
     * @param inputEntity
     * @param user
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(PolicyGroupInfoEntity inputEntity, SysUserEntity user) {
        PolicyGroupSaveConverter.handleBeforeSave(inputEntity);
        PolicyGroupInfoEntity entity = thisUpdateProvider.save(inputEntity, user.getId());
        //保存后的处理
        this.handleAfterSave(entity, user);
    }

    /**
     * 删除副本
     *
     * @param id
     * @param version
     * @param user
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteHistory(Integer id, Integer version, SysUserEntity user) {
        thisUpdateProvider.deleteHistory(id, version, user.getId());
    }

    /**
     * 批量删除
     *
     * @param ids
     * @param user
     * @return
     */
    @Override
    public Map deleteBatch(Integer[] ids, SysUserEntity user) {
        DeleteResultModel deleteResultModel = thisUpdateProvider.deleteBatch(ids, user.getId());
        //处理删除后的缓存更新
        handleCacheAfterDelete(deleteResultModel);
        if (R.SUCCESS.equals(deleteResultModel.getResult())) {
            return R.ok().msg(commonLanguageProperties.getDeleteSuccess());
        } else {
            return R.fail(deleteResultModel.getResult());
        }
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        PolicyGroupInfoUpdateServiceImpl.commonLanguageProperties = commonLanguageProperties;
    }
    /*************** ===私有方法=== ********************* ===start=== ***************************************/
    /**
     * 处理删除后的缓存更新
     *
     * @param deleteResultModel
     */
    private void handleCacheAfterDelete(DeleteResultModel deleteResultModel) {
        if (CollectionUtils.isEmpty(deleteResultModel.getIds())) {
            return;
        }
        for (Integer id : deleteResultModel.getIds()) {
            myUpdateCacheService.updateMultiCenterCache(id, ResourceTypeEnum.POLICY_GROUP.getValue());
        }
    }

    /**
     * 保存后的处理
     *
     * @param entity
     * @param user
     */
    private void handleAfterSave(PolicyGroupInfoEntity entity, SysUserEntity user) {

        //提交审核
        if (ResourceStatusValidator.isToAuditing(entity.getStatus())) {
            AuditTaskInfoEntity taskInfoEntity = AuditUtil.openTask(entity.getPolicyGroupName(), entity.getId(),
                    entity.getCurrentVersion(), ResourceTypeEnum.POLICY_GROUP.getValue(), "", user, 1);
//            AuditTaskEmailInfo auditTaskEmailInfo = AuditTaskEmailInfo.getAuditTaskEmailInfo(entity.getPolicyGroupName(), "策略组管理",
//                    user.getUserName(), taskInfoEntity.getCreatedDate());
//            if (SystemProperties.SystemConstants.ENABLE_SEND_EMAIL) {
//                //发送邮件通知审核人
//                sendEmailService.sentAuditEmail(auditTaskEmailInfo);
//            }
        }
    }
}
